package com.ruoyi.web.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.Scheduling;
import com.ruoyi.system.service.ISchedulingService;
import com.ruoyi.system.service.impl.SysDeptServiceImpl;
import com.ruoyi.system.service.impl.SysUserServiceImpl;
import com.ruoyi.web.controller.tool.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 排班信息Controller
 *
 * @author ruoyi
 * @date 2023-07-04
 */
@Api(tags = "排班信息模块")
@RestController
@RequestMapping("/system/Scheduling")
public class SchedulingController extends BaseController {
    @Autowired
    private ISchedulingService schedulingService;

    private class Wrap implements Serializable {
        Scheduling Scheduling;
        String DeptId;
        String UserId;

        @JsonProperty("DeptId")
        public String getDeptId() {
            return DeptId;
        }

        @JsonProperty("UserId")
        public String getUserId() {
            return UserId;
        }

        @JsonProperty("Scheduling")
        public com.ruoyi.system.domain.Scheduling getScheduling() {
            return Scheduling;
        }
    }

    @Autowired
    SysDeptServiceImpl sysDeptService;
    @Autowired
    SysUserServiceImpl sysUserService;

    private Wrap convert(Scheduling origin) {
        Wrap tempWrap = new Wrap();
        tempWrap.UserId = sysUserService.selectUserById(origin.getUserId()).getUserName();
        tempWrap.DeptId = sysDeptService.selectDeptById(origin.getDeptId()).getDeptName();
        tempWrap.Scheduling = origin;
        return tempWrap;
    }

    /**
     * 查询排班信息列表
     */
    @ApiOperation(value = "查询排班信息列表")
    @PreAuthorize("@ss.hasPermi('system:Scheduling:list')")
    @GetMapping("/list")
    public Page<Wrap> list(Scheduling entity, Page<Scheduling> page) {

        LambdaQueryWrapper<Scheduling> lambdaQueryWrapper = new LambdaQueryWrapper<Scheduling>();
        Page<Scheduling> old=schedulingService.page(page, lambdaQueryWrapper);
        Page<Wrap> pageWrap = new Page<>();
        List<Wrap> wraps=new ArrayList<>();
        List<Scheduling> object=old.getRecords();
        for ( Scheduling item : object) {
            wraps.add(convert(item));
        }
        pageWrap.setRecords(wraps);
        PageUtil pageUtil=new PageUtil();
        pageUtil.setPage(pageWrap,old);
        return pageWrap;
    }

    /**
     * 导出排班信息列表
     */
    @ApiOperation(value = "导出排班信息列表")
    @PreAuthorize("@ss.hasPermi('system:Scheduling:export')")
    @Log(title = "排班信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Scheduling scheduling) {
        LambdaQueryWrapper<Scheduling> queryWrapper = new LambdaQueryWrapper<>();
        List<Scheduling> list = schedulingService.list(queryWrapper);
        ExcelUtil<Scheduling> util = new ExcelUtil<Scheduling>(Scheduling.class);
        util.exportExcel(response, list, "排班信息数据");
    }


    /**
     * 获取排班信息详细信息
     */
    @ApiOperation(value = "获取排班信息详细信息")
    @PreAuthorize("@ss.hasPermi('system:Scheduling:query')")
    @GetMapping(value = "/{Id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(schedulingService.getById(id));
    }

    /**
     * 新增排班信息
     */
    @ApiOperation(value = "新增排班信息")
    @PreAuthorize("@ss.hasPermi('system:Scheduling:add')")
    @Log(title = "排班信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Scheduling scheduling) {
        return toAjax(schedulingService.save(scheduling));
    }

    /**
     * 修改排班信息
     */
    @ApiOperation(value = "修改排班信息")
    @PreAuthorize("@ss.hasPermi('system:Scheduling:edit')")
    @Log(title = "排班信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Scheduling scheduling) {
        return toAjax(schedulingService.updateById(scheduling));
    }


    /**
     * 删除排班信息
     */
    @ApiOperation(value = "删除排班信息")
    @PreAuthorize("@ss.hasPermi('system:Scheduling:remove')")
    @Log(title = "排班信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{Ids}")
    public AjaxResult remove(@PathVariable Long[] Ids) {
        return toAjax(schedulingService.removeBatchByIds(Arrays.asList(Ids)));
    }
}
